package com.tepth.maintenancesystem.api;

import android.content.Context;
import android.net.Uri;

import com.tepth.maintenancesystem.bean.MaintainDriverAndRemark;
import com.tepth.maintenancesystem.bean.MaintainListWork;
import com.tepth.maintenancesystem.bean.User;
import com.tepth.maintenancesystem.bean.VehicleMaintain;
import com.tepth.maintenancesystem.untils.DateUtils;
import com.tepth.maintenancesystem.untils.Untils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LHQ on 2016/12/12.
 */

public class MaintainHttp {

    public static final int RESULT_REFRESHLIST = 2001;
    public static final int RESULT_REFERSHOBJECT = 2002;

    public static int BARCODE_RESULTCODE = 5001;
    public static int NFC_RESULTCODE = 5002;
    public static int RETURN_BARCODE=5003;

    /**
     * 获取保养列表
     *
     * @param context
     * @param startDateStr
     * @param endDateStr
     * @param outListener
     * @return
     */
    public static List<VehicleMaintain> getMtList(Context context, String startDateStr, String endDateStr, OutLogin outListener) {
        try {
            User users = Untils.getUserList(context).get(Untils.getUserIndex(context));
            String url=HttpClient.BASE_URL+"vm/" + "Maintenance.ashx?platform=SmartFam&action=GetList&authKey="+users.getAuthKey()+"&startTime="+startDateStr+"&endTime="+endDateStr;
            //String url = HttpClient.BASE_URL + "vm/" + "Maintenance.ashx?platform=SmartFam&action=GetList&authKey=" + users.getAuthKey() + "&startTime=2016-12-16&endTime=2016-12-17";
            String resultStr = HttpClient.sendGet(url, null);
            JSONObject jsonObject = new JSONObject(resultStr);
            String code = jsonObject.getString("ResultCode");
            List<VehicleMaintain> list = new ArrayList<>();
            if (code.equals(HttpClient.SUCCESS)) {
                JSONArray resultObjs = jsonObject.getJSONArray("Result");
                for (int i = 0; i < resultObjs.length(); i++) {
                    list.add(new VehicleMaintain(resultObjs.getJSONObject(i)));
                }
                return list;
            } else if (code.equals(HttpClient.ERROR_USER)) {
                outListener.outLogin();
                return null;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static MaintainDriverAndRemark getDriverAndRemark(Context context, String maintenanceId) {
        MaintainDriverAndRemark result_obj;
        try {
            User users = Untils.getUserList(context).get(Untils.getUserIndex(context));
            String url = HttpClient.BASE_URL + "vm/" + "Maintenance.ashx?platform=SmartFam&action=GetDriverAndRemark&authKey=" + users.getAuthKey() + "&maintenanceId=" + maintenanceId;
            String result = HttpClient.sendGet(url, null);
            JSONObject jsonObject = new JSONObject(result);
            String code = jsonObject.getString("ResultCode");
            if (code.equals(HttpClient.SUCCESS)) {
                result_obj = new MaintainDriverAndRemark(jsonObject.getJSONObject("Result"));
                return result_obj;
            } else if (code.equals(HttpClient.ERROR_USER)) {
                return new MaintainDriverAndRemark();
            } else {
                return new MaintainDriverAndRemark();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new MaintainDriverAndRemark();
        }
    }

    /**
     * 开始保养
     *
     * @param context
     * @param maintenanceId
     * @return
     */
    public static String updateDriverAndRemark(Context context, String maintenanceId) {
        String resultStr = "";
        try {
            User users = Untils.getUserList(context).get(Untils.getUserIndex(context));
            //192.168.1.11:8095/vm/Maintenance.ashx?platform=SmartFam&action=UpdateDriverAndRemark&authKey=18f108c7e192483da751812edcd9cc25&maintenanceId=809&driverName=小明&remark=测试信息
            String url = HttpClient.BASE_URL + "vm/" + "Maintenance.ashx?platform=SmartFam&action=UpdateBeginWork&authKey=" + users.getAuthKey() + "&maintenanceId=" + maintenanceId;
            String result = HttpClient.sendGet(url, null);
            JSONObject jsonObject = new JSONObject(result);
            String code = jsonObject.getString("ResultCode");
            if (code.equals(HttpClient.SUCCESS)) {
                resultStr = code;
            } else if (code.equals(HttpClient.ERROR_USER)) {
                resultStr = code;
            } else {
                resultStr = jsonObject.getString("ResultMsg");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultStr = HttpClient.ERROR_NET;
        }
        return resultStr;
    }

    /**
     * 获取作业列表
     *
     * @param context
     * @param maintenanceId
     * @return
     */
    public static ArrayList<MaintainListWork> getListWork(Context context, int maintenanceId,OutLogin outLogin) {
        try {
            User users = Untils.getUserList(context).get(Untils.getUserIndex(context));
            String url = HttpClient.BASE_URL + "vm/" + "Maintenance.ashx?platform=SmartFam&action=GetListWorkContentsNormal&authKey=" + users.getAuthKey() + "&maintenanceId=" + maintenanceId;
            String resultStr = HttpClient.sendGet(url, null);
            JSONObject jsonObject = new JSONObject(resultStr);
            String code = jsonObject.getString("ResultCode");
            ArrayList<MaintainListWork> list = new ArrayList<>();
            if (code.equals(HttpClient.SUCCESS)) {
                JSONArray resultObjs = jsonObject.getJSONArray("Result");
                for (int i = 0; i < resultObjs.length(); i++) {
                    list.add(new MaintainListWork(resultObjs.getJSONObject(i)));
                }
                JSONArray resultRefer = jsonObject.getJSONArray("Refer");
                Untils.saveMaintianRefer(context, resultRefer);
                return list;
            } else if (code.equals(HttpClient.ERROR_USER)) {
                outLogin.outLogin();
                return null;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取竣工列表
     *
     * @param context
     * @param maintenanceId
     * @return
     */
    public static ArrayList<MaintainListWork> getListCompleted(Context context, int maintenanceId) {
        try {
            User users = Untils.getUserList(context).get(Untils.getUserIndex(context));
            String url = HttpClient.BASE_URL + "vm/" + "Maintenance.ashx?platform=SmartFam&action=GetListWorkContentsCompleted&authKey=" + users.getAuthKey() + "&maintenanceId=" + maintenanceId;
            String resultStr = HttpClient.sendGet(url, null);
            JSONObject jsonObject = new JSONObject(resultStr);
            String code = jsonObject.getString("ResultCode");
            ArrayList<MaintainListWork> list = new ArrayList<>();
            if (code.equals(HttpClient.SUCCESS)) {
                JSONArray resultObjs = jsonObject.getJSONArray("Result");
                for (int i = 0; i < resultObjs.length(); i++) {
                    list.add(new MaintainListWork(resultObjs.getJSONObject(i)));
                }
                JSONArray resultRefer = jsonObject.getJSONArray("Refer");
                Untils.saveMaintianRefer(context, resultRefer);
                return list;
            } else if (code.equals(HttpClient.ERROR_USER)) {
                return null;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 更新保养作业项
     *
     * @param context
     * @param maintenanceId
     * @param type
     * @param strJson
     * @return
     */
    public static String updateWorkContentsAndConfirm(Context context, int maintenanceId, int type, String strJson) {
        try {
            User users = Untils.getUserList(context).get(Untils.getUserIndex(context));
            String url_content = "";
            switch (type) {
                case 0://作业人内容
                    url_content = "&workContentsJson=" + strJson + "&confirmJson=";
                    break;
                case 1://竣检或过检
                    url_content = "&workContentsJson=&confirmJson=" + strJson;
                    break;
                case 2:
                    url_content = strJson;
                    break;
            }
            String url=HttpClient.BASE_URL + "vm/" + "Maintenance.ashx";
            String params="platform=SmartFam&action=UpdateWorkContentsAndConfirm&authKey=" + users.getAuthKey() + "&maintenanceId=" + maintenanceId + url_content + "&completedRemark=无";
            //String url = HttpClient.BASE_URL + "vm/" + "Maintenance.ashx?platform=SmartFam&action=UpdateWorkContentsAndConfirm&authKey=" + users.getAuthKey() + "&maintenanceId=" + maintenanceId + url_content + "&completedRemark=无";
            String resultStr = HttpClient.sendPost(url, params);
            JSONObject jsonObject = new JSONObject(resultStr);
            String code = jsonObject.getString("ResultCode");
//            if (code.equals(HttpClient.SUCCESS)) {
//                return HttpClient.SUCCESS;
//            } else if (code.equals(HttpClient.ERROR_USER)) {
//                return "";
//            } else {
//                return "";
//            }
            return code;
        } catch (Exception e) {
            e.printStackTrace();
            return HttpClient.ERROR_NET;
        }
    }

    /**
     * 获取某个车辆的保养历史纪录
     *
     * @param context
     * @param vehicleNumber
     * @return
     */
    public static List<VehicleMaintain> getListHistory(Context context, String vehicleNumber) {
        try {
            User users = Untils.getUserList(context).get(Untils.getUserIndex(context));
            String url = HttpClient.BASE_URL + "vm/" + "Maintenance.ashx?platform=SmartFam&action=GetList&authKey=" + users.getAuthKey() + "&startTime=" + DateUtils.getDateBeforeYear() + "&endTime=" + DateUtils.getNowCalendar(0) + "&vehicleNumber=" + vehicleNumber;
            String resultStr = HttpClient.sendGet(url, null);
            JSONObject jsonObject = new JSONObject(resultStr);
            String code = jsonObject.getString("ResultCode");
            List<VehicleMaintain> list = new ArrayList<>();
            if (code.equals(HttpClient.SUCCESS)) {
                JSONArray resultObjs = jsonObject.getJSONArray("Result");
                for (int i = 0; i < resultObjs.length(); i++) {
                    list.add(new VehicleMaintain(resultObjs.getJSONObject(i)));
                }
                return list;
            } else if (code.equals(HttpClient.ERROR_USER)) {
                return null;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static ArrayList<String> getDicNotFinishWork(Context context) {
        ArrayList<String> dateList = new ArrayList<>();
        try {
            User users = Untils.getUserList(context).get(Untils.getUserIndex(context));
            String url = HttpClient.BASE_URL + "vm/" + "Maintenance.ashx?platform=SmartFam&action=GetDicNotFinishWork&authKey=" + users.getAuthKey() + "&startDate=" + DateUtils.getBeforeMonthCalendar(-3) + "&endDate=" + DateUtils.getNowCalendar(0);
            String resultStr = HttpClient.sendGet(url, null);
            JSONObject jsonObject = new JSONObject(resultStr);
            String code = jsonObject.getString("ResultCode");
            if (code.equals(HttpClient.SUCCESS)) {
                JSONObject resultObj = jsonObject.getJSONObject("Result");
                JSONArray names = resultObj.names();
                for (int i = 0; i < names.length(); i++) {
                    dateList.add(names.getString(i));
                }
            } else if (code.equals(HttpClient.ERROR_USER)) {

            } else {

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateList;
    }
}
